package srx.open.com.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.im.BmobUserManager;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.UpdateListener;
import srx.open.com.R;
import srx.open.com.bean.User;
import srx.open.com.utils.CommonUtils;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;

/**
 * 忘记密码.
 *
 * @author hailongqiu 356752238@qq.com
 */
public class ForgotPassActivity extends ExtendBaseActivity {

    private static final String TAG = "ForgotPassActivit";

    // 标题栏
    @Bind(R.id.right_lay)
    View right_lay;
    @Bind(R.id.left_tv)
    TextView left_tv;

    // 验证过程显示
    @Bind(R.id.one_tv)
    TextView one_tv;
    @Bind(R.id.two_tv)
    TextView two_tv;
    @Bind(R.id.three_tv)
    TextView three_tv;

    //
    @Bind(R.id.forgot_pass_one_lay)
    LinearLayout forgot_pass_one_lay;
    @Bind(R.id.forgot_pass_two_lay)
    LinearLayout forgot_pass_two_lay;
    @Bind(R.id.forgot_pass_three_lay)
    LinearLayout forgot_pass_three_lay;

    @Bind(R.id.ok_ver_code_btn)
    Button ok_ver_code_btn;
    @Bind(R.id.ver_code_btn)
    Button ver_code_btn; // 获取验证码.
    @Bind(R.id.ok_forgot_pass_btn)
    Button ok_forgot_pass_btn; // 重置密码

    @Bind(R.id.account_et)
    EditText account_et;
    @Bind(R.id.ver_code_et)
    EditText ver_code_et;
    @Bind(R.id.pass_et)
    EditText pass_et;
    @Bind(R.id.pass_rept_et)
    EditText pass_rept_et;

    @Override
    public void initAllDatas() {

    }

    @Override
    public void initAllViews() {
        setContentView(R.layout.activity_forgot_pass);
        ButterKnife.bind(this);
        right_lay.setVisibility(View.GONE);
        left_tv.setText("忘记密码");
        //
        account_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                accountEtAfterTextChange(s);
            }
        });
        //
        ver_code_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                verCodeEtAfterTextChange(s);
            }
        });
        //
        pass_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                passEtAfterTextChange(s);
            }
        });
        //
        pass_rept_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                passReptEtAfterTextChange(s);
            }
        });
    }

    /*
     * 返回.
     */
    @OnClick(R.id.left_lay)
    void onLeftLayClick(View view) {
        finish();
    }

    /*
     * 获取验证码.
     */
    @OnClick(R.id.ver_code_btn)
    void ver_code_btn(View view) {
        //
        phoneStr = account_et.getText() + "";
        BmobSMS.requestSMSCode(mContext, "" + phoneStr, "forgetPass", new RequestSMSCodeListener() {
            @Override
            public void done(Integer integer, BmobException e) {
                if (e == null) {
                    OpenToast.showMsg("验证码已发送");
                    // 切换到短信验证界面.
                    one_tv.setTextColor(mResource.getColor(R.color.black));
                    two_tv.setTextColor(mResource.getColor(R.color.head_bg));
                    // 切换短信验证界面.
                    forgot_pass_one_lay.setVisibility(View.GONE);
                    ver_code_btn.setVisibility(View.GONE);
                    forgot_pass_two_lay.setVisibility(View.VISIBLE);
                    ok_ver_code_btn.setVisibility(View.VISIBLE);
                } else {
                    Log.e(TAG,  ">>> " + e.getErrorCode() + " " + e.getMessage() + "");
                    OpenToast.showMsg("验证码发送失败");
                }
            }
        });
    }

    /*
     * 验证验证码
     */
    @OnClick(R.id.ok_ver_code_btn)
    void ok_ver_code_btn(View view) {
        String verCode = ver_code_et.getText().toString();
        /* 验证短信 [这里就登陆成功了，然后修改密码] */
        BmobUser.loginBySMSCode(this, phoneStr, verCode, new LogInListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (user != null) {
                    // 切换到短信验证界面.
                    two_tv.setTextColor(mResource.getColor(R.color.black));
                    three_tv.setTextColor(mResource.getColor(R.color.head_bg));
                    // 切换短信验证界面.
                    forgot_pass_two_lay.setVisibility(View.GONE);
                    ok_ver_code_btn.setVisibility(View.GONE);
                    forgot_pass_three_lay.setVisibility(View.VISIBLE);
                    ok_forgot_pass_btn.setVisibility(View.VISIBLE);
                } else {
                    OpenToast.showMsg("验证码错误" + e.getMessage() + " code:" + e.getErrorCode() + " phoneStr:" + phoneStr);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BmobUserManager.getInstance(mContext).logout();
    }

    /**
     * 重置密码.
     */
    @OnClick(R.id.ok_forgot_pass_btn)
    void ok_forgot_pass_btn(View view) {
        BmobUser currentUser = BmobUser.getCurrentUser(mContext);
        currentUser.setPassword(pass_et.getText() + "");
        currentUser.update(mContext, new UpdateListener() {
            @Override
            public void onSuccess() {
                OpenToast.showMsg("密码重置成功");
                finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                OpenToast.showMsg("密码重置失败:" + msg);
            }
        });
    }

    /**
     * 手机号码
     */
    void accountEtAfterTextChange(Editable text) {
        // 检验是否为手机号码.
        if (CommonUtils.isMobileNO(text.toString())) {
            ver_code_btn.setEnabled(true);
        } else {
            ver_code_btn.setEnabled(false);
        }
    }

    /**
     * 验证码.
     */
    void verCodeEtAfterTextChange(Editable text) {
        // 判断验证码是否不空.
        if (!TextUtils.isEmpty(text.toString())) {
            ok_ver_code_btn.setEnabled(true);
        } else {
            ok_ver_code_btn.setEnabled(false);
        }
    }

    String password = "";

    /*
     * 重置密码输入
     */
    void passEtAfterTextChange(Editable text) {
        String reptPass = pass_rept_et.getText().toString();
        password = text.toString();
        if (!TextUtils.isEmpty(password) && password.equals(reptPass)) {
            ok_forgot_pass_btn.setEnabled(true);
        } else {
            ok_forgot_pass_btn.setEnabled(false);
        }
    }

    /*
     * 重置密码确认输入.
     */
    void passReptEtAfterTextChange(Editable text) {
        String reptPass = text.toString();
        if (!TextUtils.isEmpty(password) && password.equals(reptPass)) {
            ok_forgot_pass_btn.setEnabled(true);
        } else {
            ok_forgot_pass_btn.setEnabled(false);
        }
    }

}
